<?php

/**
 * Albireo Kernel
 *
 * @copyright  Copyright (c) 2010 Albireo Solutions
 * @package    Kernel
 */
namespace KernelModule\LogsModule\PhpModule;

use KernelModule\BasePresenter;

use Nette\Environment;
use Nette\Debug;

use Albireo\FileSystem\FileException;
use KernelModule\LogsModule\LogsException;
use KernelModule\LogsModule\NotEnoughDiskSpaceException;
use KernelModule\LogsModule\NotAnySpaceForLogsException;

/**
 * Presenter for processing PHP error logs (dirty to clean).
 *
 * @author     Pavel
 * @package    Kernel
 */
class ProcessPresenter extends BasePresenter {

    const displayPresenter = 'Display:';
    public $processModel;
    
    public function handleProcess(){
        $this->invalidateControl('procCount');
        //$this->redirect('this');
    }
    
    public function actionRefreshAzureLogs() {
       $azureLogsModel = new AzureLogs();
       $logDir = Environment::expand('%logDir%');
       $azureLogsModel->deleteLocalLogs($logDir);
       try {
            $azureLogsModel->downloadLogsFromAzure();
       } catch (NotEnoughDiskSpaceException $ex) {
           $this->flashMessage($ex->getMessage()); //TODO: how to get the message to stay on the screen longer
       } catch (NotAnySpaceForLogsException $ex) {
           $this->flashMessage($ex->getMessage());
           $this->redirect("default");
       }
       $azureLogsModel->moveLogsFromTemp();
       $this->redirect("default");
    }

    public function actionDefault() {
        if ($this->processModel === NULL) {
            $this->processModel = new Process();
        }
        if (!$this->processModel->workOrDirtyExist()) { //no dirty or work file
            $this->redirect($this::displayPresenter);
        }
        try {
            if ($this->processModel->processDirty()) { //whole file read
                $this->redirect($this::displayPresenter);
            } else $this->template->processed = $this->processModel->getProcessed();
        } catch (FileException $e) {
            //$this->redirect(':Kernel:Logs:', $e->getMessage());
            $this->redirect(':Kernel:Logs:Error:');
        } catch (LogsException $e) {
            $this->redirect(':Kernel:Logs:Error:fsProblem');
        }
    }

    public function renderDefault() {
        $this->showMenu();
        $this->template->title = _("Processing");
    }


}